package DataStructures;

/**
 * One Array with two stack 3.24
 * @author xing
 * @version 1.0 2015-4-28
 * @param <T>
 */

public class OneArrayWithTwoStack<T> {
	public OneArrayWithTwoStack(int size) {
		theItems = (T [])new Object[size];
		topOfLowStack = -1;
		topOfHighStack = size;
	}
	public boolean pushToLowStack(T x) {
		if(topOfHighStack - topOfLowStack > 1 ) {
			theItems[++topOfLowStack] = x;
			return true;
		} else 
			return false;
	}
	public T popFromLowStack() {
		return topOfLowStack > 0 ? theItems[topOfLowStack--] : null; 
	}
	public T atPeekOfLowStack() {
		return topOfLowStack > 0 ? theItems[topOfLowStack] : null;
	}
	public boolean pushToHighStack(T x) {
		if(topOfHighStack - topOfLowStack > 1) {
			theItems[--topOfHighStack] = x;
			return true;
		} else return false;
	}
	public T popFromHighStack() {
		return topOfHighStack < theItems.length ? theItems[topOfHighStack++] : null;
	}
	public T atPeekOfHighStack() {
		return topOfHighStack < theItems.length ? theItems[topOfHighStack] : null;
	}
	
	private T [] theItems;
	private int topOfLowStack;
	private int topOfHighStack;
}
